package net.office.service.impl;

import net.office.bean.SignIn;
import net.office.bean.SignInExample;
import net.office.bean.WorkTableExample;
import net.office.mapper.SignInMapper;
import net.office.service.SignInService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class SignInServiceImpl implements SignInService {

    @Autowired
    private SignInMapper signInMapper;

    @Override
    public List<SignIn> findMonthByStaffId(Integer staffId, Date date) {
        Calendar begin_calendar = Calendar.getInstance();
        begin_calendar.setTime(date);
        begin_calendar.set(Calendar.DAY_OF_MONTH, 1);
        Date begin_date = begin_calendar.getTime();

        Calendar end_calendar = Calendar.getInstance();
        end_calendar.setTime(date);
        end_calendar.add(Calendar.MONTH, 1);
        end_calendar.set(Calendar.DAY_OF_MONTH, 0);
        Date end_date = end_calendar.getTime();

        SignInExample example = new SignInExample();
        example.createCriteria().andSignStaffIdEqualTo(staffId).andSignDateBetween(begin_date, end_date);

        return signInMapper.selectByExample(example);
    }

    @Override
    public List<SignIn> findMonthByDate(Date date) {
        Calendar begin_calendar = Calendar.getInstance();
        begin_calendar.setTime(date);
        begin_calendar.set(Calendar.DAY_OF_MONTH, 1);

        Date begin_date = begin_calendar.getTime();

        Calendar end_calendar = Calendar.getInstance();
        end_calendar.add(Calendar.MONTH, 1);
        end_calendar.set(Calendar.DAY_OF_MONTH, 0);
        end_calendar.set(Calendar.HOUR, 23);
        end_calendar.set(Calendar.MINUTE, 59);
        end_calendar.set(Calendar.SECOND, 59);
        Date end_date = end_calendar.getTime();

        SignInExample example = new SignInExample();
        example.createCriteria().andSignDateBetween(begin_date, end_date);

        return signInMapper.selectByExample(example);
    }

    @Override
    public SignIn findOneByDate(Date date) {
        Calendar begin_calendar = Calendar.getInstance();
        begin_calendar.setTime(date);
        begin_calendar.set(Calendar.HOUR, 0);
        begin_calendar.set(Calendar.MINUTE, 0);
        begin_calendar.set(Calendar.SECOND, 0);

        Date begin_date = begin_calendar.getTime();

        Calendar end_calendar = Calendar.getInstance();
        end_calendar.add(Calendar.MONTH, 1);
        end_calendar.set(Calendar.DAY_OF_MONTH, 0);
        end_calendar.set(Calendar.HOUR, 23);
        end_calendar.set(Calendar.MINUTE, 59);
        end_calendar.set(Calendar.SECOND, 59);
        Date end_date = end_calendar.getTime();

        SignInExample example = new SignInExample();
        example.createCriteria().andSignDateBetween(begin_date, end_date);

        List<SignIn> signInList = signInMapper.selectByExample(example);

        if(signInList.size() != 0){
            return signInList.get(0);
        }
        return null;
    }

    @Override
    public int insert(SignIn signIn) {
        if(findOneByDate(signIn.getSignDate())!=null){
            return 1;
        }
        return signInMapper.insert(signIn);
    }
}
